<?php
session_start();

if(!isset($_SESSION['last_key'])) {
  echo '{"error_code":0, "error":"Not logged in"}';
  return;
}

include_once('config.php');
include_once('weibooauth.php');

$c = new WeiboClient(WB_AKEY, WB_SKEY, $_SESSION['last_key']['oauth_token'], $_SESSION['last_key']['oauth_token_secret']);

$page    = isset($_GET['page']) ? $_GET['page'] : WB_START_PAGE;
$count   = isset($_GET['count']) ? $_GET['count'] : WB_COUNT_EACH_PAGE;

$msg = $c->comments_to_me($page, $count);

$msg_count = count($msg);
for ($i = 0; $i < $msg_count; ++$i) {
  $slim[$i]['id'] = $msg[$i]['id'];
  $slim[$i]['text'] = $msg[$i]['text'];
  $slim[$i]['created_at'] = $msg[$i]['created_at'];
  $slim[$i]['user']['id'] = $msg[$i]['user']['id'];
  $slim[$i]['user']['name'] = $msg[$i]['user']['name'];
  $slim[$i]['user']['verified'] = $msg[$i]['user']['verified'];
  $slim[$i]['status']['text'] = $msg[$i]['status']['text'];
  if (isset($msg[$i]['reply_comment'])) {
    $slim[$i]['reply_comment']['id'] = $msg[$i]['reply_comment']['id'];
    $slim[$i]['reply_comment']['text'] = $msg[$i]['reply_comment']['text'];
    $slim[$i]['reply_comment']['created_at'] = $msg[$i]['reply_comment']['created_at'];
    $slim[$i]['reply_comment']['user']['id'] = $msg[$i]['reply_comment']['user']['id'];
    $slim[$i]['reply_comment']['user']['name'] = $msg[$i]['reply_comment']['user']['name'];
    $slim[$i]['reply_comment']['user']['verified'] = $msg[$i]['reply_comment']['user']['verified'];
    $slim[$i]['reply_comment']['status']['text'] = $msg[$i]['reply_comment']['status']['text'];
  }
}

if ($page == WB_START_PAGE) {
  $c->clear_unread(1);//comments
}

echo json_encode($slim);
?>